package com.effectiv.crm.forms;

import java.io.IOException;
import java.util.List;

import javax.annotation.PostConstruct;
import javax.inject.Inject;
import javax.inject.Named;

import jodd.cache.FIFOCache;

import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;
import org.springframework.core.io.ResourceLoader;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;

import lombok.extern.slf4j.Slf4j;

import com.thoughtworks.xstream.XStream;

@Slf4j
@Named
public class FormLoader {

	@Inject
	private ResourceLoader resourceLoader;

	@PostConstruct
	public void load() {
		log.info("---- LOADING FORMS -----");
		List<Form> forms = loadXml();
		loadDb();
	}

	protected List<Form> loadXml() {
		XStream xstream = new XStream();
		xstream.processAnnotations(Form.class);
		//xstream.autodetectAnnotations(true);

		PathMatchingResourcePatternResolver resolver = new PathMatchingResourcePatternResolver();
		try {
			Resource resources[] = resolver.getResources("classpath*:/META-INF/form*.xml");
			
			log.info("resources : {}", resources);

			for (Resource r : resources) {
				
				log.info("Loading form resource : {}", r.getFilename());
				
				Form f = (Form) xstream.fromXML(r.getInputStream());

			}

		} catch (IOException e) {
			log.error("Unable to load forms", e);

		}

		return null;
	}

	protected List<Form> loadDb() {
		return null;
	}
}
